﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Service.QixingManage;
using YiSha.Model.Result.QixingManage;
using YiSha.Enum.OrganizationManage;
using YiSha.Enum.QixingManage;
using YiSha.Enum;

namespace YiSha.Business.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2023-03-19 19:47
    /// 描 述：订单管理业务类
    /// </summary>
    public class OrderMasterBLL
    {
        private OrderMasterService orderMasterService = new OrderMasterService();

        private OrderDetailService orderDetailService = new OrderDetailService();

        private GoodsService  goodsService = new GoodsService();

        private UmsMemberService umsMember = new UmsMemberService();

        #region 获取数据
        public async Task<TData<List<OrderMasterEntity>>> GetList(OrderMasterListParam param)
        {
            TData<List<OrderMasterEntity>> obj = new TData<List<OrderMasterEntity>>();
            obj.Data = await orderMasterService.GetList(param);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<OrderMasterEntity>>> GetPageList(OrderMasterListParam param, Pagination pagination)
        {
            TData<List<OrderMasterEntity>> obj = new TData<List<OrderMasterEntity>>();
            obj.Data = await orderMasterService.GetPageList(param, pagination);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<OrderMasterEntity>> GetEntity(long id)
        {
            TData<OrderMasterEntity> obj = new TData<OrderMasterEntity>();
            obj.Data = await orderMasterService.GetEntity(id);
            if (obj.Data != null)
            {
                obj.Tag = 1;
            }
            return obj;
        }

        public TData<OrderDto> GetMasterOrderEntity(long id)
        {
            TData<OrderDto> obj = new TData<OrderDto>();
            var data= orderDetailService.GetGoodsOrderEntity(id);

            data.source_type_doc = data.source_type.GetDescriptionByEnum<source_type_enum>();

            data.order_status_doc = data.order_status.GetDescriptionByEnum<orderStatus_enum>();

            data.payment_method_doc= data.payment_method.GetDescriptionByEnum<payment_method_enum>();

            data.order_type_doc = data.order_type.GetDescriptionByEnum<order_type_enum>();

            obj.Data = data;

            if (obj.Data != null)
            {
                obj.Tag = 1;
            }
            return obj;
        }

        #endregion

        #region 提交数据
        public async Task<TData<string>> SaveShippingProductForm(OrderMasterEntity entity) 
        {
            TData<string> obj = new TData<string>();

            if (entity.IsNullOrZero())
            {
                obj.Message = "订单异常";
                obj.Tag =0;
                return obj;
            }

            var orderinfo=orderMasterService.GetEntity(entity.Id.Value).Result;

            if (orderinfo==null)
            {
                obj.Message = "订单异常";
                obj.Tag = 0;
                return obj;
            }

            //减库存改
            var des=await orderMasterService.BaseRepository().FindList<OrderDetailEntity>(t=>t.order_id==entity.Id);

            foreach (var item in des)
            {
                var pwids=TextHelper.SplitToArray<long>(item.productids, ',');

                var pws=orderMasterService.BaseRepository().FindList<GoodsWarehouseEntity>(t => pwids.Contains(t.Id.Value)).Result.ToList();

                foreach (var pw in pws)
                {
                    if (pw != null && pw.goods_type == 1)
                    {
                        pw.status = 1;
                    }
                    else
                    {
                        pw.status = 2;
                    }
                    await orderMasterService.BaseRepository().Update(pw);
                }

                //减少颜色的库存
                //var color = orderMasterService.BaseRepository().FindList<GoodsAttrEntity>(t => t.Id == item.colorid).Result.FirstOrDefault();
                //color.count = color.count - pws.Count;
                //await orderMasterService.BaseRepository().Update(color);
            }

            orderinfo.delivery_company= entity.delivery_company;
            orderinfo.delivery_sn=entity.delivery_sn;
            //确认收货状态：0->未确认；1->已确认
            orderinfo.confirm_status = ((int)confirm_status.未确认);
            orderinfo.order_status = ((int)OrderStatusEnum.已发货);
            orderinfo.shipping_time=DateTime.Now;
            await orderMasterService.SaveForm(orderinfo);

            

            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<string>> SaveForm(OrderMasterEntity entity)
        {
            TData<string> obj = new TData<string>();

            await orderMasterService.SaveForm(entity);

            //判断订单状态
            if (entity.order_status == ((int)orderStatus_enum.已完成))
            {
                try
                {
                    await orderMasterService.BaseRepository().BeginTrans();

                    var ordermaster=orderMasterService.GetEntity(entity.Id.Value).Result;

                    //更新货品库存
                    var data = orderDetailService.GetQueryOrderEntity(entity.Id.Value).Result;

                    //var goodsinfo = goodsService.GetEntity(data.goods_id.Value).Result;

                    //goodsinfo.goods_buyCount = goodsinfo.goods_buyCount + data.goods_cnt;

                    //await goodsService.SaveForm(goodsinfo);

                    //新增积分(骑行币)
                    var userMember=umsMember.GetUserIdByEntity(ordermaster.customer_id.Value).Result;

                    if (userMember!=null) {

                        var qixingbi = (int)Math.Round(entity.payment_money.Value, 0);

                        userMember.history_integration = userMember.history_integration + qixingbi;

                        userMember.integration = userMember.integration + qixingbi;

                        await umsMember.SaveForm(userMember);
                    }

                    await orderMasterService.BaseRepository().CommitTrans();
                }
                catch (Exception e)
                {
                    await orderMasterService.BaseRepository().RollbackTrans();
                }
            }

            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await orderMasterService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 私有方法
        #endregion
    }
}
